#include "common/header.h"
#include "common/application.h"

CApplication::CApplication(void)
	: m_bContinue( TRUE )
	, m_WindowWidth( 800 )
	, m_WindowHeight( 600 )
	, m_hIcon( NULL )
	, m_hIconSm( NULL )
{
}

CApplication::~CApplication(void)
{
}

ATOM CApplication::MyRegisterClass(HINSTANCE hInstance, WNDPROC pfnWndProc)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= pfnWndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= m_hIcon;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= m_szWindowClass;
	wcex.hIconSm		= m_hIconSm;

	return RegisterClassEx(&wcex);
}

xgc_bool CApplication::InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   m_hInst = hInstance; // Store instance handle in our global variable

   m_hWnd = CreateWindow(
	   m_szWindowClass, 
	   m_szTitle, 
	   WS_OVERLAPPEDWINDOW, 
	   CW_USEDEFAULT, 0, 
	   CW_USEDEFAULT, 0, 
	   NULL, 
	   NULL, 
	   hInstance, 
	   NULL);

   if (!m_hWnd)
   {
		DWORD dwErr = GetLastError();
		return FALSE;
   }

   ShowWindow(m_hWnd, nCmdShow);
   UpdateWindow(m_hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CApplication::HandlerMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if( !OnMessage( hWnd, message, wParam, lParam ) )
	{
		int wmId, wmEvent;
		PAINTSTRUCT ps;
		HDC hdc;

		switch (message)
		{
		case WM_NCCREATE:
			if( !OnCreate( hWnd ) )
				return DefWindowProc( hWnd, message, wParam, lParam );
			break;
		case WM_COMMAND:
			wmId    = LOWORD(wParam);
			wmEvent = HIWORD(wParam);
			if( !OnCommand( wmId, wmEvent, lParam ) )
				return DefWindowProc( hWnd, message, wParam, lParam );
			break;
		case WM_PAINT:
			hdc = BeginPaint(m_hWnd, &ps);
			// TODO: Add any drawing code here...
			OnPaint( ps );
			EndPaint(m_hWnd, &ps);
			break;
		case WM_SIZE:
			OnSize( wParam, LOWORD( lParam ), HIWORD( lParam ) );
			break;
		case WM_DESTROY:
			m_bContinue = FALSE;
			OnDestroy();
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
	}
	return 0;
}

int CApplication::Initialize(HINSTANCE hInstance, int nCmdShow, WNDPROC pfnWndProc )
{
	// Initialize global strings
	//LoadString(hInstance, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);
	//LoadString(hInstance, IDS_APPLICATION, m_szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance, pfnWndProc );

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	// m_hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR));
	return TRUE;
}

int CApplication::Go()
{
	MSG msg;

	// Main message loop:
	while(m_bContinue)
	{
		if(GetMessage(&msg, NULL, 0, 0))
		{
			if (!TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}

	return (int) msg.wParam;
}